<?php
/**
 * Created by 无所谓已
 * User: Administrator
 * Date: 2025-09-20
 * Time: 12:23
 */
namespace plugin\csauto\app\model;

use plugin\admin\app\model\Base;

/**
 * @property integer $id         ID(主键)
 * @property array   $scope_site_id   站点ID范围 (JSON字段)
 */
class SiteRulesModel extends Base
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'cs_site_rules';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'id';

    // 添加这一行来声明 scope_site_id 属性
    protected $scope_site_id;
    
    protected $casts = [
        'scope_site_id' => 'array',  // 自动处理JSON字段
    ];

    /**
     * 设置 scope_site_id 属性
     *
     * @param string|array $value
     */
    public function setScopeSiteIdAttribute($value)
    {
        if (is_string($value)) {
            // 将逗号分隔的字符串转换为数组
            $arrayValue = explode(',', $value);
            // 转换为整数（可选）
            $arrayValue = array_map('intval', $arrayValue);
            $this->attributes['scope_site_id'] = json_encode($arrayValue);
        } else {
            // 让 $casts 处理其他情况
            $this->scope_site_id = $value;
        }
    }
}